package com.rabbiter.pm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rabbiter.pm.domain.Accident;
import com.rabbiter.pm.domain.Car;
import com.rabbiter.pm.domain.User;
import com.rabbiter.pm.domain.vo.AccidentSelectVo;
import com.rabbiter.pm.domain.vo.AccidentVo;
import com.rabbiter.pm.domain.vo.HandleAccidentVo;
import com.rabbiter.pm.mapper.AccidentMapper;
import com.rabbiter.pm.mapper.CarMapper;
import com.rabbiter.pm.mapper.UserMapper;
import com.rabbiter.pm.service.AccidentService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AccidentServiceImpl extends ServiceImpl<AccidentMapper,Accident>  implements AccidentService {

    @Resource
    private AccidentMapper accidentMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private CarMapper carMapper;

    @Override
    public Object reportAccident(AccidentVo accidentVo) {

        Map<String,Object> res = new HashMap<>();
        if (accidentVo.getUid() == null){
            res.put("msg","用户id不能为空");
            res.put("flag",false);
            return res;
        }
        if (accidentVo.getTid() == null){
            res.put("msg","车辆id不能为空");
            res.put("flag",false);
            return res;
        }
        if (accidentVo.getPlace() == null){
            res.put("msg","事故地点不能为空");
            res.put("flag",false);
            return res;
        }
        Accident accident = new Accident();
        accident.setAccidentTime(LocalDateTime.now());
        accident.setPlace(accidentVo.getPlace());
        accident.setUid(accidentVo.getUid());
        accident.setTid(accidentVo.getTid());
        accident.setAccidentRes(accidentVo.getAccidentRes());
        accident.setIsSuccess("0");
        // 通过uid查询用户
        User user = userMapper.selectById(accidentVo.getUid());
        user.setPassword(null);
        // 通过tid查询车辆
        Car car = carMapper.selectById(accidentVo.getTid());
        // 插入数据库
        int insert = accidentMapper.insert(accident);
        if(insert > 0){
            res.put("flag",true);
            res.put("msg","上报成功");
            res.put("user",user);
            res.put("car",car);
            return res;
        }
        return res;
    }

    @Override
    public Object selectAll(AccidentSelectVo accidentSelectVo) {
        Map<String,Object> res = new HashMap<>();
        QueryWrapper<Accident> accidentQueryWrapper = new QueryWrapper<>();
        if (accidentSelectVo.getIsSuccess() != null) {
            accidentQueryWrapper.like("is_success", accidentSelectVo.getIsSuccess());
        }
        if (accidentSelectVo.getPlace() != null) {
            accidentQueryWrapper.like("place", accidentSelectVo.getPlace());
        }
        if (accidentSelectVo.getAccidentRes() != null) {
            accidentQueryWrapper.like("accident_res", accidentSelectVo.getAccidentRes());
        }
        List<Accident> accidents = accidentMapper.selectList(accidentQueryWrapper);
        for (Accident accident : accidents) {
            // 假设您有一个UserMapper和一个CarMapper来查询用户和车辆信息
            User user = userMapper.selectById(accident.getUid());
            Car car = carMapper.selectById(accident.getTid());
            user.setPassword(null);
            // 设置用户和车辆信息到事故对象中
            accident.setUser(user);
            accident.setCar(car);
        }

        if(accidents.size() > 0){
            res.put("msg","查询成功");
            res.put("flag",true);
            res.put("accidents",accidents);
            return res;
        }
        return res;
    }

    @Override
    public Object dealAccident(HandleAccidentVo handleAccidentVo) {
        // 处理事故
        Map<String,Object> res = new HashMap<>();
        if (handleAccidentVo.getId() == null){
            res.put("msg","事故id不能为空");
            res.put("flag",false);
            return res;
        }
        if (handleAccidentVo.getUid() == null){
            res.put("msg","用户id不能为空");
            res.put("flag",false);
            return res;
        }
        Integer id = handleAccidentVo.getId();
        System.out.println(handleAccidentVo.getUid());
//        Accident accident = accidentMapper.selectById(handleAccidentVo.getId());
        Accident accident = accidentMapper.selectById(id);
        if (accident == null){
            res.put("msg","事故不存在");
            res.put("flag",false);
            return res;
        }

        accident.setIsSuccess(handleAccidentVo.getIsSuccess());
        accident.setAccidentTime(LocalDateTime.now());
        accident.setHandleRes(handleAccidentVo.getHandleRes());
        int i = accidentMapper.updateById(accident);
        // 处理人信息查询
        User user = userMapper.selectById(handleAccidentVo.getUid());
        user.setPassword(null);
        accident.setUser(user);
        if(i > 0){
            res.put("msg","处理成功");
            res.put("flag",true);
            return res;
        }
        return res;
    }
}
